package com.action;

import com.bean.Admin;
import com.bean.Student;
import com.bean.Teacher;
import com.bean.User;
import com.dao.AdminDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.opensymphony.xwork2.ActionSupport;
import com.util.HttpUtil;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6434128483294080524L;
	
	private User user;
	
	boolean b = false;
	//private Dao dao = new UserDao();
	
	public String login(){
		
		if(user.getStatus()==1){
			System.out.println("student login!");
			StudentDao sd = new StudentDao();
			Student stu = new Student();
			stu.setStuUserID(Integer.parseInt(user.getUserName()));
			stu.setStuPassword(user.getPassword());
			if(sd.findStu(stu)==null){
				
				this.addFieldError("user", "用户名密码错误");
				return ERROR;
			}else if(sd.findStu(stu)!=null){
				HttpUtil.setSession("stu", stu);
				HttpUtil.setSession("type", "1");
				b = true;
				return SUCCESS;
				
			}
		}else if(user.getStatus()==2){
			System.out.println("teacher login!");
			TeacherDao td = new TeacherDao();
			Teacher tea = new Teacher();
			tea.setTeaUserID(Integer.parseInt(user.getUserName()));
			tea.setTeaPassword(user.getPassword());
			if(td.findTea(tea)==null){
				this.addFieldError("user", "用户名密码错误");
				return ERROR;
			}else if(td.findTea(tea)!=null){
				HttpUtil.setSession("tea", tea);
				HttpUtil.setSession("type", "2");
				b = true;
				return SUCCESS;
			}
			
		}else if(user.getStatus()==3){
			System.out.println("admin login!");
			AdminDao sd = new AdminDao();
			Admin adm = new Admin();
			adm.setAdmUserID(Integer.parseInt(user.getUserName()));
			adm.setAdmPassWord(user.getPassword());
			if(sd.findAdm(adm)==null){
				this.addFieldError("user", "用户名密码错误");
				return ERROR;
			}else if(sd.findAdm(adm)!=null){
				HttpUtil.setSession("adm", adm);
				HttpUtil.setSession("type", "3");
				b = true;
				System.out.println("登陆");
				return INPUT;
			}
		}
		return SUCCESS;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public boolean isB() {
		return b;
	}

	public void setB(boolean b) {
		this.b = b;
	}
	
			
	
}
